<template>
	<div>
		<div>
			<db></db>
		</div>
		<div>
			<van-nav-bar class="fbb" style="background: #d12d1a;"  title="发布"  />
		</div>
		<div>
			<van-cell-group>
				<van-field label="标题" v-model="xzmz.title" placeholder="请输入标题" />
				<!-- <van-field label="内容" v-model="xzmz.content" placeholder="请输入发布的内容" /> -->

                <van-field
  v-model="xzmz.content"
  rows="2"
  autosize
  label="内容"
  type="textarea"
  maxlength="150"
  placeholder="请输入发布的内容"
  show-word-limit
/>


			</van-cell-group>
			<van-field readonly clickable label="位置" :value="xzmz.cate_name" placeholder="请选择发布位置"
				@click="showPicker = true" />
			<van-popup v-model="showPicker" round position="bottom">
				<van-picker show-toolbar :columns="dx" value-key="name" @cancel="showPicker = false"
					@confirm="onConfirm" />
			</van-popup>
			<van-uploader  v-model="fileList" multiple :max-count="3" />
			<van-button @click="fbb">发布</van-button>
		</div>
	</div>
</template>

<script>
	import db from '../../components/dibu/db.vue'
  import {scjk,scQN} from '@/api/sctp'
  
  import {fzsctp} from '@/api/sctp'

  import config from '@/config/index.js'
	import {
		mapState
	} from 'vuex'
	import {
		fbsj
	} from '@/api/fbjk';
	export default {
		components: {
			db
		},
		computed: {
			...mapState('mkhfl', ['dx']),
			...mapState('mkhuser', ['uid', 'userInfo']),
		},
		data() {
			return {
				fileList:[],
				showPicker: false,
				xzmz: {
					title: '',
					content: '',
					cate_id: '',
					cate_name: '',
					author: '',
					author_id: '',
					imageSrc: []
				}
			}
		},
		methods: {
      // scmz(type){
      //       let name=Math.random().toString(16).slice(2)
      //       name+=(+new Date())
      //       if(type){
      //           return name+'.'+type
      //       }
      //       return name
      // },
			async fbb() {
				if(this.xzmz.title==''){
                      this.$toast('请输入标题')
					  return
				}
				if(this.xzmz.content==''){
                     this.$toast('请输入要发布的内容')
					 return
				}
				if(this.xzmz.cate_id==''){
                  this.$toast('请选择要发布的位置')
				}
				let {
					uid,
					userInfo,
					xzmz,
          fileList
				} = this
				if (!uid) {
					this.$toast('您还没有登录请登录')
					setTimeout(() => {
						this.$router.push('dlu')
					}, 1000)
					return
				}
				  xzmz.author_id = uid
				xzmz.author = userInfo.username
        console.log(fileList)
        if(fileList.length==2){
           this.$toast('请上传一张或三张')
           return
        }
        if(fileList.length>0){
			let xh=fileList.map((z,xb)=>{
				console.log(z)
				return fzsctp(fileList[xb].file)
			}) 
			console.log(xh)
			xzmz.imageSrc=await Promise.all(xh)
			console.log(xzmz)
        }
			  
				
		fbsj(
					xzmz
				).then(res => {
					console.log(res)
					if (res.code == 0) {
						this.$toast(res.msg)
						this.$router.push('sy')
					} else {
						this.$toast(res.msg)
					}
				}).catch((err) => {
					console.log(err)
				});
			


				// console.log(xzmz)
				
			},
			onConfirm({
				name,
				_id
			}) {
				this.showPicker = false
				// console.log(e)
				this.xzmz.cate_id = _id
				this.xzmz.cate_name = name
				console.log(this.xzmz)
			},
			
		}
	}
</script>

<style scoped>
::v-deep .van-nav-bar__title{
	color: rgb(255,255,255); 
}

</style>
